
local runtime        = require 'jass.runtime'

local card = {
    __tostring = function(self)
        local player = self.player
        if player then
            return ('% 的抽卡逻辑[%s]'):format(player,self.name)
        end
        return ('抽卡逻辑[%s]'):format(self.name)
    end,
}
setmetatable(card, card)
ac.card_logic = card
local mt = {}
card.__index = mt

--抽卡显示ui
mt.select_ui = nil

--刷新显示ui
mt.replace_ui = nil

--刷新数量
mt.show_count = 3


--[执行事件]
function mt:notify(name, ...)
    local func = self[name]
    if func then
        return select(2, xpcall(func, runtime.error_handle, self, ...))
    end
end

function mt:show_select(data,show)
    local player = self.player
    local ui = self.select_ui
    self._select_list = data
    if player:is_self() and ui then
        if show then
            ui:set_data(data,0.15)
        else
            if ui:is_show() then
                ui:hide()
            else
                ui:set_data(data)
            end
        end
    end
end

function mt:show_replace(temp)
    local player = self.player
    local data = self._card_save
    self._replace_card = temp
    if player:is_self() and self.replace_ui then
        self.replace_ui:set_data(data,temp)
    end
end


--[抽卡]
function mt:used(name)
    local player = self.player
    local pool = self._card_pool or {}
    local data = {}
    --将展示的退回卡池
    for a,temp in ipairs(self._select_list or {}) do
        table.insert(pool,temp)
    end
    for a=1,self.show_count do
        local temp,index = table.random(pool)
        table.remove(pool,index)
        data[a] = temp
    end
    self.select_class = name
    self:notify('show_select',data,true)
end

--[选择]
function mt:select(index)
    local data = self._select_list
    local temp = data and data[index]
    if temp then
        self._select_list = nil
        --将剩余的退回卡池
        for a,temp in ipairs(data) do
            if a~=index then
                table.insert(self._card_pool,temp)
            end
        end
        self:notify('on_add',temp.name)
    end
end

--[获得逻辑]
function mt:add_card(name,index)
    local player = self.player
    local hero = player.hero
    local list = self._card_save
    self:remove_card(index)

    local skill = hero:add_skill(name)
    list[index] = skill
    self:notify('finish',list[index])
    return skill
end

function mt:remove_card(index)
    local list = self._card_save
    local old = list[index]
    if old then
        local name = old.bind_name or old.name
        old:remove()
        self:add(name,-1)
        
        local pool = self:get_pool()
        local skill = ac.lni('skill',name)
        table.insert(pool,skill)
    end
end


--[获得前]
function mt:on_add(name)
    local player = self.player
    local list = self._card_save
    for a=1,6 do
        if list[a]==nil then
            self:add_card(name,a)
            return
        end
    end

    local temp = ac.skill(name)
    
    self:notify('show_replace',temp)
end

--[替换]
function mt:replace(index)
    local player = self.player
    local list = self._card_save
    local temp = self._replace_card
    self._replace_card = nil
    if temp and index>0 and index<=6 then
        self:add_card(temp.name,index)
    end
end


--[添加到队列执行]
function mt:queue_run(name)
    local data = self._select_list
    local queue = self._card_queue or {}
    local name = name or #queue
    table.insert(queue,name)
    if data==nil then
        self:notify('run',name)
    end
end

--[检查队列继续执行]
function mt:queue_check()
    local queue = self._card_queue or {}
    table.remove(queue,1)
    if #queue>0 then
        self:notify('used',queue[1])
    end
end


--[完成抽卡]
function mt:finish(skill)
    self:add(skill.bind_name or skill.name,1)
    self:notify('update_pool',skill)
    self:notify('queue_check')
end

--[更新卡池]
function mt:update_pool()

end

--[获取卡池]
function mt:get_pool()
    return self._card_pool
end

--[获取当前选择表]
function mt:get_list()
    return self._select_list
end

--[获取数量]
function mt:get(name)
    return self._has_card[name] or 0
end

--[设置数量]
function mt:set(name,num)
    self._has_card[name] = num
end

--[增加数量]
function mt:add(name,num)
    self:set(name,self:get(name) + num)
end

--[拥有列表]
function mt:get_save(name,num)
    return self._card_save
end


--[初始化]
function mt:init()
    self._has_card = {}
    self._card_queue = {}
    self._card_save = {}
    self._card_pool = {}
    self:notify('update_pool')
end




return card

